class Solution {
public:
    int reverse(int x) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        int sign = x < 0 ? 1 : 0;
        if (x < 0) {
            x = -x;
        }
        int r = 1;
        while (x / (r * 10) > 0) {
            r *= 10;
        }
        int ans = 0;
        while (x > 0) {
            ans += (x % 10) * r;
            x /= 10;
            r /= 10;
        }
        return sign ? -ans : ans;
    }
};
